package com.tantan.client;

import com.tantan.client.ClientScanner;
import com.tantan.model.ExaminationQuestions;

import java.io.*;
import java.util.LinkedList;
import java.util.List;

/**
 * 自定义类实现管理员系统的考题的管理
 */
public class ExaminationQuestionsManagement {

    /**
     * 自定义成员变量记录流对象
     */
    private ObjectInputStream ois;
    private ObjectOutputStream oos;
    /**
     * 自定义试题类集合用来装所有试题对象
     */
    List<ExaminationQuestions> list = new LinkedList<>();

    /**
     * 自定义成员方法实现考题管理模块界面的绘制
     */
        public void examinationQuestionManagementPage() throws IOException, ClassNotFoundException {
            while (true){
                System.out.println("             考题管理界面");
                System.out.println("---------------------------------------");
                System.out.println("     [1] 增加考题       [2] 删除考题");
                System.out.println("     [3] 修改考题       [4] 查找考题");
                System.out.println("     [5] 退出界面");
                System.out.println("---------------------------------------");
                System.out.println("请输入要进行的业务编号：");
                int choose = ClientScanner.gteScanner().nextInt();
                switch (choose){
                    case 1:
                        increaseExaminationquestions();
                        break;
                    case 2:
                        deleteExaminationquestions();
                        break;
                    case 3:
                        changeExaminationquestions();
                        break;
                    case 4:
                        findExaminationquestions();
                        break;
                    case 5:
                        System.out.println("正在退出考题管理模块....");
                        return;
                    default:
                        System.out.println("输入错误，请重新选择！");
                }
            }
        }

    /**
     * 自定义成员方法实现增加考题
     */
    private void increaseExaminationquestions() throws IOException {
        System.out.println("请输入考题的编号");
        int i = ClientScanner.gteScanner().nextInt();
        System.out.println("请输入考题的内容:");
        String exam = ClientScanner.gteScanner().next();
        System.out.println("请输入考题的答案：");
        String answer = ClientScanner.gteScanner().next();
        ExaminationQuestions eq = new ExaminationQuestions(i,exam,answer);
        oos = new ObjectOutputStream(new FileOutputStream("C:\\fileTest\\exam.txt"));
        oos.writeObject(eq);
        System.out.println("考题增加成功！");
    }


    /**
     * 自定义成员方法实现根据题号删除考题
     */
    private void deleteExaminationquestions() throws IOException, ClassNotFoundException {
        ois = new ObjectInputStream(new FileInputStream("C:\\fileTest\\exam.txt"));
        list.add((ExaminationQuestions) ois.readObject());
        System.out.println("请输入要删除试题的编号：");
        int i1 = ClientScanner.gteScanner().nextInt();
        File file = new File("C:\\fileTest\\exam.txt");
        oos = new ObjectOutputStream(new FileOutputStream("C:\\fileTest\\exam.txt"));
        for (ExaminationQuestions e:list) {
            if(i1 == e.getSerialNumber()){
                list.remove(e);
                file.delete();
            }
        }
        for (ExaminationQuestions e:list) {
            oos.writeObject(e);
        }
        System.out.println("考题删除成功！");
    }

    /**
     * 自定义成员方法实现修改考题
     */
    private void changeExaminationquestions() throws IOException, ClassNotFoundException {
        ois = new ObjectInputStream(new FileInputStream("C:\\fileTest\\exam.txt"));
        list.add((ExaminationQuestions) ois.readObject());
        System.out.println("请输入要修改试题的编号：");
        int i3 = ClientScanner.gteScanner().nextInt();
        File file = new File("C:\\fileTest\\exam.txt");
        oos = new ObjectOutputStream(new FileOutputStream("C:\\fileTest\\exam.txt"));
        int i4 = 0;
        for (ExaminationQuestions e:list) {
            if(i3 == e.getSerialNumber()){
                file.delete();
                list.remove(e);
                System.out.println("请输入修改后的考题:");
                String exam = ClientScanner.gteScanner().next();
                System.out.println("请输入答案：");
                String answer = ClientScanner.gteScanner().next();
                list.add(i4,new ExaminationQuestions(i4+1,exam,answer));
            }
            i4++;
        }
        for (ExaminationQuestions e:list) {
            oos.writeObject(e);
        }
        System.out.println("考题修改成功！");
    }

    /**
     * 自定义成员方法实现查找考题
     */
    private void findExaminationquestions() throws IOException, ClassNotFoundException {
        System.out.println("请输入要查找的题号：");
        int i5 = ClientScanner.gteScanner().nextInt();
        ois = new ObjectInputStream(new FileInputStream("C:\\fileTest\\exam.txt"));
        list.add((ExaminationQuestions) ois.readObject());
        for (ExaminationQuestions e:list) {
            if(i5 == e.getSerialNumber()){
                System.out.println("该题为：" + e.getExam());
                System.out.println("答案是：" + e.getAnswer());
            }
        }
    }
}
